package com.xiaomi.ad.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.miui.zeus.mimo.sdk.ad.AdWorkerFactory;
import com.miui.zeus.mimo.sdk.ad.IAdWorker;
import com.miui.zeus.mimo.sdk.listener.MimoAdListener;
import com.xiaomi.ad.common.pojo.AdType;
import com.xiaomi.ad.mimo.demo.R;

public class NewsFeedListActivity extends Activity {
    public static final String TAG = "List-StandardNewsFeed";

    private static final String POSITION_ID = "2cae1a1f63f60185630f78a1d63923b0";

    // 本demo中广告将会均匀分布在item中，同时第一个item不会是广告
    private static final int maxItemsSize = 30;
    private static final int maxAdSize = 4;

    private int mRealAds = 0;
    private LayoutInflater mInflater;
    private IAdWorker mWorker;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.standard_news_feed_list_layout);
        mListView = findViewById(R.id.adList);
        mInflater = LayoutInflater.from(this);

        findViewById(R.id.fetchAd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListView.setAdapter(null);
                recycle();
                prepareAd();
            }
        });
        findViewById(R.id.showAd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListView.setAdapter(new MyListAdapter());
                mListView.setVisibility(View.VISIBLE);
                findViewById(R.id.showAd).setEnabled(false);
            }
        });
    }

    private void prepareAd() {
        try {
            mWorker = AdWorkerFactory.getAdWorker(getApplicationContext(), null, new MimoAdListener() {
                @Override
                public void onAdPresent() {
                }

                @Override
                public void onAdClick() {
                }

                @Override
                public void onAdDismissed() {
                }

                @Override
                public void onAdFailed(String s) {
                }

                @Override
                public void onAdLoaded(int i) {
                    Log.e(TAG, "onAdLoaded num : " + i);
                    mRealAds = i;
                    findViewById(R.id.showAd).setEnabled(true);
                }

                @Override
                public void onStimulateSuccess() {
                }
            }, AdType.AD_STANDARD_NEWSFEED);
            mWorker.load(POSITION_ID, maxAdSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void finish() {
        super.finish();
        try {
            // 这里必须先清空list再回收广告，不然会fc
            mListView.setAdapter(null);
            recycle();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void recycle() {
        try {
            mWorker.recycle();
        } catch (Exception e) {
        }
    }

    class MyListAdapter extends BaseAdapter {
        final static int TYPE_AD = 0;
        final static int TYPE_NORMAL_ITEM = 1;


        @Override
        public int getCount() {
            return maxItemsSize;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private boolean isAdPosition(int index) {
            int internal = maxItemsSize / mRealAds;
            // 第1个item不放广告
            if ((index + 1) % internal == 0
                    && index != 0) {
                return true;
            }
            return false;
        }

        @Override
        public int getItemViewType(int position) {
            if (isAdPosition(position)) {
                return TYPE_AD;
            }
            return TYPE_NORMAL_ITEM;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            try {
                if (isAdPosition(position)) {
                    int internal = maxItemsSize / mRealAds;
                    convertView = mWorker.updateAdView(convertView, position / internal);
                } else {
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.list_item_layout, null);
                    }
                }
                return convertView;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
